function main() {
    debugger
    const self = this;
    const formPartEle = self.$$("form_part_agocyp");
    const formRenderEle = formPartEle && formPartEle.$$getRenderElement();
    const humanId = formRenderEle && formRenderEle.getDocumentNo();

    if (!formPartEle || !formRenderEle) return;

    if (humanId) {
        // 编辑现有记录
        handleEdit(formRenderEle, self, humanId).catch(function (error) {
            self.$message.error("操作失败");
            console.error("编辑操作出错:", error);
        });
    } else {
        // 新增记录
        handleCreate(formRenderEle, self).catch(function (error) {
            self.$message.error("操作失败");
            console.error("新增操作出错:", error);
        });
    }
}

function handleEdit(formRenderEle, self, humanId) {
    return formRenderEle.onSave()
        .then(function (res) {
            if (!res || !res.result) return;
            self.$message.success("提交成功");
            // return formRenderEle.$$setExtraParams({documentNo: res.result.documentNo});
            let pageRenderElement = self.$$root().$parent.$parent.$parent;
            return pageRenderElement.goHistory(-1);
        })
        .then(function () {
            return syncHumanToUserCenter(humanId);
        });
}

function handleCreate(formRenderEle, self) {
    return api.EXEC_FORMULA(
        "ENTITY_SELECT_ONE('t_4916fdfaf0174ad19ff7e4ed5355c90a',enable_flag,QUERY_EQ(id,1))"
    ).then(function (response) {
        if (response.result === "1") {
            return handleCreateWithApproval(formRenderEle, self);
        }
        return handleDirectCreate(formRenderEle, self);
    });
}

function handleCreateWithApproval(formRenderEle, self) {
    return formRenderEle.onSave()
        .then(function (res) {
            if (!res || !res.result) return;
            const humanId = res.result.documentNo;
            self.$message.success("提交成功");
            return Promise.all([
                insertApproval(humanId),
                syncHumanToUserCenter(humanId)
            ]).then(() => {
                let pageRenderElement = self.$$root().$parent.$parent.$parent;
                return pageRenderElement.goHistory(-1);
            });

            // return formRenderEle.$$setExtraParams({documentNo: humanId})
            //     .then(function () {
            //         return Promise.all([
            //             insertApproval(humanId),
            //             syncHumanToUserCenter(humanId)
            //         ]);
            //     });
        });
}

function handleDirectCreate(formRenderEle, self) {
    const setValues = [
        self.$$("number_input_d0ez1e").$$setValue("1"),
        self.$$("text_input_9th1gf").$$setValue("在职"),
        self.$$("text_input_7ctixh").$$setValue("1")
    ];

    return Promise.all(setValues)
        .then(function () {
            return formRenderEle.onSave();
        })
        .then(function (res) {
            if (!res || !res.result) return;
            const humanId = res.result.documentNo;
            self.$message.success("提交成功");
            return syncHumanToUserCenter(humanId, self);
            // return formRenderEle.$$setExtraParams({documentNo: humanId})
            //     .then(function () {
            //         return syncHumanToUserCenter(humanId);
            //     });
        });
}

function syncHumanToUserCenter(params, self) {
    const syncHuman = [{
        name: "id",
        valueContent: params
    }];
    return api.DATA_SOURCE_EXECUTE(
        "ds_api:7a33e6eb-1ea6-46d3-be49-afca3a903a58",
        "api",
        syncHuman,
        [],
        ""
    ).then(function (res) {
        const resValue = JSON.parse(res.result);
        console.log("同步用户中心成功", resValue);
        return resValue;
    }).then(() => {
        let pageRenderElement = self.$$root().$parent.$parent.$parent;
        pageRenderElement.goHistory(-1);
    }).catch(function (error) {
        console.error("同步用户中心失败:", error);
        throw error;
    });
}

function insertApproval(humanUUID) {
    const insertApprovalSql = "INSERT('t_655b23a930504145bf2ba2f014780dc6',['human_uuid','" + humanUUID + "','approval_type_code',1,'approval_type_name','人员新增','create_human_id',USER_ID(),'create_time',NOW()])";
    console.log(insertApprovalSql);
    return api.EXEC_FORMULA(insertApprovalSql)
        .then(function (res) {
            console.log("新增人员审核成功", res);
            return res;
        })
        .catch(function (error) {
            console.error("新增人员审核失败:", error);
            throw error;
        });
}
